import type { BattleCard, BattleAttr } from '../types';

export const getCardAttr = (card: BattleCard): BattleAttr => {
  return {
    HP: card.battle.HP,
    ATK: getCardATK(card),
    DEF: getCardDEF(card),
    SPD: card.battle.SPD,
    HIT: getCarHIT(card),
    PRY: getCarPRY(card),
    CRIT: getCardCRIT(card),
    CRIRES: getCardCRIRES(card),
    DMGADD: getCardDMGADD(card),
    DMGRED: getCardDMGRED(card),
    STUN: card.battle.STUN,
    STUNRES: card.battle.STUNRES,
    ANGRY: card.battle.ANGRY,
    CTRL: card.battle.CTRL,
  };
};

export const getCardATK = (card: BattleCard): number => {
  const { base, buff } = card;
  const { attack } = base;

  const addBuff = buff.filter(v => v.BUFF === 1 && v.EFF === 4);
  const reduceBuff = buff.filter(v => v.BUFF === 1 && v.EFF === 3);

  const add = addBuff.reduce((a, b) => a + b.VAL, 0);
  const reduce = reduceBuff.reduce((a, b) => a + b.VAL, 0);

  const buffValue = (add - reduce) / 1000;

  return Math.round(attack * (1 + buffValue));
};

export const getCardDEF = (card: BattleCard): number => {
  const { base, buff } = card;
  const { defence } = base;

  const addBuff = buff.filter(v => v.BUFF === 3 && v.EFF === 4);
  const reduceBuff = buff.filter(v => v.BUFF === 3 && v.EFF === 3);

  const add = addBuff.reduce((a, b) => a + b.VAL, 0);
  const reduce = reduceBuff.reduce((a, b) => a + b.VAL, 0);

  const buffValue = (add - reduce) / 1000;

  return Math.round(defence * (1 + buffValue));
};

const getCarHIT = (card: BattleCard): number => {
  const { battle, buff } = card;
  const { HIT } = battle;

  const addBuff = buff.filter(v => v.BUFF === 6 && v.EFF === 4);
  const reduceBuff = buff.filter(v => v.BUFF === 6 && v.EFF === 3);

  const add = addBuff.reduce((a, b) => a + b.VAL, 0);
  const reduce = reduceBuff.reduce((a, b) => a + b.VAL, 0);

  const buffValue = add - reduce + HIT;

  return Math.round(buffValue);
};

const getCarPRY = (card: BattleCard): number => {
  const { battle, buff } = card;
  const { PRY } = battle;

  const addBuff = buff.filter(v => v.BUFF === 6 && v.EFF === 4);
  const reduceBuff = buff.filter(v => v.BUFF === 6 && v.EFF === 3);

  const add = addBuff.reduce((a, b) => a + b.VAL, 0);
  const reduce = reduceBuff.reduce((a, b) => a + b.VAL, 0);

  const buffValue = add - reduce + PRY;

  return Math.round(buffValue);
};

const getCardCRIT = (card: BattleCard): number => {
  const { battle, buff } = card;
  const { CRIT } = battle;

  const addBuff = buff.filter(v => v.BUFF === 7 && v.EFF === 4);
  const reduceBuff = buff.filter(v => v.BUFF === 7 && v.EFF === 3);

  const add = addBuff.reduce((a, b) => a + b.VAL, 0);
  const reduce = reduceBuff.reduce((a, b) => a + b.VAL, 0);

  const buffValue = add - reduce + CRIT;

  return Math.round(buffValue);
};

const getCardCRIRES = (card: BattleCard): number => {
  const { battle, buff } = card;
  const { CRIRES } = battle;

  const addBuff = buff.filter(v => v.BUFF === 8 && v.EFF === 4);
  const reduceBuff = buff.filter(v => v.BUFF === 8 && v.EFF === 3);

  const add = addBuff.reduce((a, b) => a + b.VAL, 0);
  const reduce = reduceBuff.reduce((a, b) => a + b.VAL, 0);

  const buffValue = add - reduce + CRIRES;

  return Math.round(buffValue);
};

const getCardDMGADD = (card: BattleCard): number => {
  const { battle, buff } = card;
  const { DMGADD } = battle;

  const addBuff = buff.filter(v => v.BUFF === 9 && v.EFF === 4);
  const reduceBuff = buff.filter(v => v.BUFF === 9 && v.EFF === 3);

  const add = addBuff.reduce((a, b) => a + b.VAL, 0);
  const reduce = reduceBuff.reduce((a, b) => a + b.VAL, 0);

  const buffValue = add - reduce + DMGADD;

  return Math.round(buffValue);
};

const getCardDMGRED = (card: BattleCard): number => {
  const { battle, buff } = card;
  const { DMGRED } = battle;

  const addBuff = buff.filter(v => v.BUFF === 10 && v.EFF === 4);
  const reduceBuff = buff.filter(v => v.BUFF === 10 && v.EFF === 3);

  const add = addBuff.reduce((a, b) => a + b.VAL, 0);
  const reduce = reduceBuff.reduce((a, b) => a + b.VAL, 0);

  const buffValue = add - reduce + DMGRED;

  return Math.round(buffValue);
};
